{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a7a57404-6162-4279-954e-714d88b31759",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n",
      "t统计量: -0.44274195148073325\n",
      "p值: 0.6684021817926444\n",
      "在5.0%的显著性水平下，不能拒绝零假设，没有足够的证据认为该厂生产的灯泡的平均寿命不等于1600小时。\n"
     ]
    }
   ],
   "source": [
    "print('信计221 刘显婷 224180117')\n",
    "import numpy as np\n",
    "from scipy import stats\n",
    "# 样本数据\n",
    "data = np.array([1490, 1440, 1680, 1610, 1500, 1750, 1550, 1420, 1800, 1580])\n",
    "# 假设的总体均值\n",
    "mu_0 = 1600\n",
    "# 使用ttest_1samp计算t值和p值\n",
    "t_stat, p_value = stats.ttest_1samp(data, mu_0)\n",
    "# 打印结果\n",
    "print(f\"t统计量: {t_stat}\")\n",
    "print(f\"p值: {p_value}\")\n",
    "# 根据p值做出决策\n",
    "alpha = 0.05\n",
    "if p_value < alpha:\n",
    "    print(f\"在{alpha*100}%的显著性水平下，拒绝零假设，认为该厂生产的灯泡的平均寿命不等于1600小时。\")\n",
    "else:\n",
    "    print(f\"在{alpha*100}%的显著性水平下，不能拒绝零假设，没有足够的证据认为该厂生产的灯泡的平均寿命不等于1600小时。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "10dfbe13-3c0e-47bb-8119-4808b03397b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n",
      "T-statistic: 2.6483906406648634\n",
      "P-value: 0.014678975636689911\n",
      "两种热处理方法加工的金属材料抗拉强度有显著差异。\n"
     ]
    }
   ],
   "source": [
    "print('信计221 刘显婷 224180117')\n",
    "import numpy as np  \n",
    "from scipy import stats   \n",
    "# 数据  \n",
    "data_a = np.array([31, 34, 29, 26, 32, 35, 38, 34, 30, 29, 32, 31])  \n",
    "data_b = np.array([26, 24, 28, 29, 30, 29, 32, 26, 31, 29, 32, 28])    \n",
    "# 独立双样本t检验，假设方差相等  \n",
    "t_stat, p_value = stats.ttest_ind(data_a, data_b, equal_var=True)  \n",
    "# 输出结果  \n",
    "print(f\"T-statistic: {t_stat}\")  \n",
    "print(f\"P-value: {p_value}\")   \n",
    "# 判断是否有显著差异  \n",
    "alpha = 0.05  \n",
    "if p_value < alpha:  \n",
    "    print(\"两种热处理方法加工的金属材料抗拉强度有显著差异。\")  \n",
    "else:  \n",
    "    print(\"两种热处理方法加工的金属材料抗拉强度没有显著差异。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2163eaaf-d2e9-4d04-bbdb-85c1ed7dd57d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n",
      "两种材料的磨损度不一样。\n"
     ]
    }
   ],
   "source": [
    "print(\"信计221 刘显婷 224180117\")\n",
    "import numpy as np\n",
    "from scipy import stats\n",
    "# 读取数据\n",
    "data = np.loadtxt(\"D:/Users/19202/Desktop/shoes.txt\")\n",
    "# 进行配对双样本 t 检验\n",
    "t_statistic, p_value = stats.ttest_rel(data[:, 0], data[:, 1])\n",
    "# 判断是否有显著差异\n",
    "if p_value < 0.05:\n",
    "    print(\"两种材料的磨损度不一样。\")\n",
    "else:\n",
    "    print(\"两种材料的磨损度一样。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d78a2baf-a8ab-4394-842c-4274920b40fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计 221 刘显婷 224180117\n",
      "chi2_statistic=5.4980, pvalue=0.0190, df=1, expected_freq=[[133.76518219 456.23481781]\n",
      " [ 34.23481781 116.76518219]]\n"
     ]
    }
   ],
   "source": [
    "print(\"信计 221 刘显婷 224180117\")\n",
    "import numpy as np\n",
    "from scipy import stats\n",
    "# 创建一个二维数组作为数据\n",
    "data = np.array([[123, 467], [45, 106]])\n",
    "# 进行卡方独立性检验，False 表示不进行 Yates 校正\n",
    "result = stats.chi2_contingency(data, False)\n",
    "# 输出卡方检验的结果信息，包括卡方统计量、p 值、自由度和期望频数\n",
    "print('chi2_statistic=%.4f, pvalue=%.4f, df=%i, expected_freq=%s' % result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7dc091e3-541a-476d-897c-3f90d5dba8ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计 221 刘显婷 224180117\n",
      "Pearson 相关系数: 0.795287724092366\n",
      "Spearman 相关系数: 0.757833767163966\n",
      "Kendall τ 相关系数: 0.5952522088801563\n"
     ]
    }
   ],
   "source": [
    "print(\"信计 221 刘显婷 224180117\")\n",
    "import numpy as np\n",
    "from scipy.stats import pearsonr, spearmanr, kendalltau\n",
    "# 读取数据\n",
    "data = np.loadtxt(\"D:/Users/19202/Desktop/highschool.txt\")\n",
    "# 提取初三成绩和高一成绩\n",
    "j3 = data[:, 0]\n",
    "s1 = data[:, 1]\n",
    "# 计算 Pearson 相关系数\n",
    "pearson_corr, _ = pearsonr(j3, s1)\n",
    "# 计算 Spearman 相关系数\n",
    "spearman_corr, _ = spearmanr(j3, s1)\n",
    "# 计算 Kendall τ 相关系数\n",
    "kendall_corr, _ = kendalltau(j3, s1)\n",
    "print(f\"Pearson 相关系数: {pearson_corr}\")\n",
    "print(f\"Spearman 相关系数: {spearman_corr}\")\n",
    "print(f\"Kendall τ 相关系数: {kendall_corr}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73117f06-e693-4512-b58e-7cbd3ca852ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
